#pragma once

#ifndef __CR_IMAGE_LOADER_API_H__
#define __CR_IMAGE_LOADER_API_H__

NS_CR_BEGIN

namespace image
{
	namespace loader
	{
		using AsyncResult	= _STD future<Result>;
		using LoaderAgent	= singleton<Loader>;

		namespace priv
		{
			template<typename T>
			void set_option(SpTask& task, T&& t)
			{
				task->SetOption(_STD_FWD(t));
			}

			template <typename T, typename... Ts>
			void set_option(SpTask& task, T&& t, Ts&&... ts)
			{
				set_option(task, _STD_FWD(t));
				set_option(task, _STD_FWD(ts)...);
			}
		}

		void Init(const Configuration& config)
		{
			LoaderAgent::get_mutable_instance().Init(config);
		}
		
		void Term()
		{
			LoaderAgent::get_mutable_instance().Term();
		}

		//同步加载.(会阻塞主线程. 不建议使用)
		template<typename ...T>
		Result LoadImage(T... t)
		{
			_STD shared_ptr<TaskFragment> task = _STD make_shared<TaskFragment>();
			priv::set_option(task->getptr(), _STD_FWD(t)...);
			return LoaderAgent::get_mutable_instance().Load(_STD_FWD(task->getptr()));
		}

		//异步加载
		template<typename ...T>
		AsyncResult LoadImageAsync(T... t)
		{
			_STD shared_ptr<TaskFragment> task = _STD make_shared<TaskFragment>();
			priv::set_option(task->getptr(), _STD_FWD(t)...);
			return LoaderAgent::get_mutable_instance().LoadAsync(_STD_FWD(task->getptr()));
		}

		//带回调的异步加载方式 参数一回调 std::function<void(Result, void*)>
		template <typename Then, typename... Ts>
		AsyncResult LoadImageCallBack(Then then, Ts... ts)
		{
			_STD shared_ptr<TaskFragment> task = _STD make_shared<TaskFragment>(then);
			priv::set_option(task->getptr(), _STD_FWD(ts)...);
			return LoaderAgent::get_mutable_instance().LoadAsync(_STD_FWD(task->getptr()));
		}

		//当前正在进行的任务数
		uint32_t GetTasksCounts(void)
		{
			return LoaderAgent::get_const_instance().getTasksCount();
		}

		//当前正在排队的任务队列数 (排除正在进行中的)
		uint32_t GetTaskQueueCounts(void)
		{
			return LoaderAgent::get_const_instance().getQueuesCount();
		}
	}
}


NS_CR_END

#endif // !__CR_IMAGE_LOADER_API_H__
